<?php

class Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_ContentTypes
{
    protected $_defaults = array();
    protected $_overrides = array();
    
    public function addDefault($extension, $contentType)
    {
        $this->_defaults[$extension] = $contentType;
        
        return $this;
    }
    
    public function addOverride($partName, $contentType)
    {
        $this->_overrides[$partName] = $contentType;
        
        return $this;
    }
    
    public function toXml()
    {
        $xml = new XMLWriter();
        $xml->openMemory();
        $xml->startDocument('1.0', 'UTF-8', 'yes');
        
        $xml->startElement('Types');
        $xml->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
        
        foreach ($this->_defaults as $extension => $contentType) {
            $xml->startElement('Default');
            $xml->writeAttribute('Extension', $extension);
            $xml->writeAttribute('ContentType', $contentType);
            $xml->endElement();
        }

        foreach ($this->_overrides as $partName => $contentType) {
            $xml->startElement('Override');
            $xml->writeAttribute('PartName', $partName);
            $xml->writeAttribute('ContentType', $contentType);
            $xml->endElement();
        }
        
        $xml->endElement();
        
        $xml->endDocument();
        
        return $xml->outputMemory();
    }
}
